var equipMaVm = new Vue({
	el:'#equip-manage-content',
	data:function() {
		return{
			equipmentid: '',
			zcbh: '',
			tmbh: '',
			zybbh: '',
			type: '',
			name: '',
			vendor: '',
			model: '',
			depart:'',
			workshop: '',
			user: '',
			contact: '',
			location: '',
			note: '',
			recorder: '',
			putindate: '',
			state: '',
			showEquipmentDetail: false,
			changeData: [],
			hitchData: [],
			maintainData: [],
			showChange: false,
			showProblem: false,
			showMaintain: false,
			thisPage: null,
			showAddHitch: true,
			showAddMaintain: true,
			showAddChange: true,
			showEndHitch: false,
			showEndMaintain: false
		}
	},
	watch:{
		state:function(val) {
			this.state = val;
		}
	},
	computed:{
		stateName: function() {
			var theClassName = '';
			switch(this.state) {
				case 1:
					theClassName = '正常';
					break;
				case 2:
					theClassName = '故障中';
					break;
				case 3:
					theClassName = '维修中';
					break;
				case 0:
					theClassName = '已报废';
					break;
			}
			return theClassName;
		},
		stateClassName: function() {
			var theClassName = '';
			switch(this.state) {
				case 1:
					theClassName = 'normal';
					break;
				case 2:
					theClassName = 'in-hitch';
					break;
				case 3:
					theClassName = 'in-maintain';
					break;
				case 0:
					theClassName = 'scraped';
					break;
			}
			return theClassName;
		}
	},
	mounted: function() {
		var self = this;
		mui.init();
		mui.plusReady(function() {
			self.thisPage = plus.webview.currentWebview();
			if(self.thisPage.type == "CODE128") {
				self.tmbh = self.thisPage.code;
			} else {
				self.zcbh = self.thisPage.code;
			}
			self.initLayout();
			self.initData();
		})
	},
	methods:{
		authControl: function() {
			var authDepart = plus.storage.getItem('depart');
			var role = Number(plus.storage.getItem('role'));
			var authType = Number(plus.storage.getItem('type'));
			
			var state = Number(this.state);
			var depart = this.depart;
			//authType为1表示:用户有效, 0为无效 
			console.log("1")
			console.log("authType:" + authType)
			if(authType == 1) {
				console.log("2")
				
				console.log("role:" + role)
				console.log("authDepart:" + authDepart)
				console.log("depart:" + depart)
				
				//IT部门级用户(role == 1)和部门组长(role == 2)可以对资产进行管理(报修/故障/变更)
				if((role == 2 || role == 1 ) && authDepart == depart) {
					console.log("3")
					switch(state) {
						case 1:
							this.showAddHitch = true;
							this.showAddMaintain = true;
							this.showAddChange = true;
							this.showEndHitch = false;
							this.showEndMaintain = false;
							break;
						case 2:
							this.showAddHitch = false;
							this.showAddMaintain = false;
							this.showAddChange = false;
							this.showEndHitch = true;
							this.showEndMaintain = false;
							break;
						case 3:
							this.showAddHitch = false;
							this.showAddMaintain = false;
							this.showAddChange = false;
							this.showEndHitch = false;
							this.showEndMaintain = true;
							break;
						case 0:
							this.showAddHitch = false;
							this.showAddMaintain = false;
							this.showAddChange = false;
							this.showEndHitch = false;
							this.showEndMaintain = false;
							break;
						default:
							break;
					}
					console.log(this.showAddHitch);
				} else {
					this.showAddHitch = false;
					this.showAddMaintain = false;
					this.showAddChange = false;
					this.showEndHitch = false;
					this.showEndMaintain = false;
					return false;
				}
			} else {
				this.showAddHitch = false;
				this.showAddMaintain = false;
				this.showAddChange = false;
				this.showEndHitch = false;
				this.showEndMaintain = false;
			}
		},
		initLayout:function() {
			mui('.mui-scroll-wrapper').scroll({
				scrollY: true, //是否竖向滚动
				scrollX: false, //是否横向滚动
				startX: 0, //初始化时滚动至x
				startY: 0, //初始化时滚动至y
				indicators: true, //是否显示滚动条
				deceleration: 0.0006, //阻尼系数,系数越小滑动越灵敏
				bounce: true //是否启用回弹
			})
		},
		initData: function() {
			var self = this;
			var url = config.requestBaseUrl + 'getEquipmentInfo';
			
			mui.ajax(url,{
				data:{
					zcbh: this.zcbh,
					tmbh: this.tmbh,
				},
				dataType:'json',//服务器返回json格式数据
				type:'get',//HTTP请求类型
				timeout:5000,//超时时间设置为5秒；
				success:function(data){
					mui.toast(data.msg);
					var baseInfo = data.data.baseInfo[0];
					if(data.isSuccess) {
						console.log("状态:" + baseInfo.state)
						self.state = baseInfo.state;
						self.equipmentid = baseInfo.id,
						self.zcbh = baseInfo.zcbh;
						self.tmbh = baseInfo.tmbh;
						self.zybbh = baseInfo.zybbh;
						self.type = baseInfo.type;
						self.name = baseInfo.name;
						self.vendor = baseInfo.vendor;
						self.model = baseInfo.model;
						self.depart = baseInfo.depart;
						self.workshop = baseInfo.workshop;
						self.user = baseInfo.user;
						self.contact = baseInfo.contact;
						self.location = baseInfo.location;
						self.note = baseInfo.note;
						self.putindate = new Date(baseInfo.putindate).toLocaleDateString();
						self.user = baseInfo.user;
						self.recorder = baseInfo.recorder;
						
						self.changeData = data.data.change;
						self.hitchData = data.data.hitch;
						self.maintainData = data.data.maintain;
						console.log( "------------------------------------")
						console.log( "changeData:" + self.changeData.length )
						if(self.changeData.length > 0) {
							self.showChange = true;
							console.log("111")
						}
						console.log( "hitchData:" + self.hitchData.length )
						if(self.hitchData.length > 0) {
							self.showProblem = true;
							console.log("222")
						}
						console.log( "maintainData:" + self.maintainData.length )
						if(self.maintainData.length > 0) {
							self.showMaintain = true;
							console.log("333")
						}
						if(!self.showMaintain && !self.showProblem && !self.showChange) {
							self.showInfoDetail(true);
							console.log("444")
						}
						self.authControl();
					} else {
						mui.toast(data.msg);
					}
				},
				error:function(xhr,type,errorThrown){
					mui.toast("出错啦:" + errorThrown);
				}
			});
		},
		showInfoDetail: function(boo) {
			this.showEquipmentDetail = boo;
		},
		toHitchPage: function() {
			var hitchPage = plus.webview.create('hitch.html', 'hitch', {
				left: '0%'
			}, {
				equipmentid: this.equipmentid
			});
			hitchPage.show({
				extras: {
					equipmentid: this.equipmentid
				}
			});
			this.thisPage.hide();
		},
		toMaintainPage: function() {
			var maintainPage = plus.webview.create('maintain.html', 'maintain',{
				left: '0%'
			}, {
				equipmentid: this.equipmentid,
			});
			maintainPage.show({
				extras: {
					equipmentid: this.equipmentid
				}
			});
			this.thisPage.hide();
		},
		toChangePage: function() {
			var changePage = plus.webview.create('change.html', 'change', {
				left: '0%'
				}, {
					equipmentid: this.equipmentid
				});
			changePage.show({
				extras: {
					equipmentid: this.equipmentid
				}
			});
			this.thisPage.hide();
		},
		toCloseHitch: function() {
			var eventPage = plus.webview.create('eventHandle.html', 'eventHandle',{
				left: '0%'
			},{
				equipmentid: this.equipmentid
			});
			eventPage.show();
			this.thisPage.hide();
		},
		toCloseMaintain: function() {
			var maintainPage = plus.webview.create('maintain.html', 'maintain',{
				left: '0%'
			}, {
				equipmentid: this.equipmentid,
				isInMaintain: true
			});
			maintainPage.show();
			this.thisPage.hide();
		}
	}
})